<?php
/**
 * Description of _header_account
 * 
 * @author trongkhoi
 * @since Oct 7, 2013 - 6:28:37 PM
 */
?>
<?php if (Yii::app()->user->isGuest): ?>
    <div class="sign-block pull-right" style="margin:0; margin-right:15px;">
        <span class="welcome">Welcome</span>
        <div class="clearfix"></div>
        <div class="dropdown pull-left login-tab">
            <a class="dropdown-toggle" data-toggle="dropdown" id="btnsign_in" href="#">Sign In</a>
            <?php $this->renderPartial('//layouts/_login') ?>
        </div>
        <div class="dropdown-menu dropdown-menu-right forgot-form hide" id="forgot-form">
            <div class="row-fluid">
                <div class="span12 register-form-slogan">
                    RESET YOUR SHAMPOO PASSWORD
                </div>
            </div>
            <div class="row-fluid">
                <div class="alert align-center alert-success hide">
                </div>
                <div class="alert align-center alert-error hide">
                </div>
                <div class="fl-none margin-auto span9">
                    <div class="row-fluid">
                        <!--<div class="signin-wrapper">-->
                        <div class="clearfix-custom-20"></div>
                        <div class="row-fluid">
                            <div class="span12">
                                <?php
                                cs()->registerScriptFile(themeUrl() . '/assets/js/jquery.pnotify.min.js');
                                cs()->registerCssFile(themeUrl() . '/assets/css/jquery.pnotify.default.css');
                                $subsModel = new Users('forgotpwd');
                                $form = $this->beginWidget('CActiveForm', array(
                                    'id' => 'forgotpwd-form',
                                    'action' => Yii::app()->controller->createUrl('/frontend/site/forgotPass'),
                                    // Please note: When you enable ajax validation, make sure the corresponding
                                    // controller action is handling ajax validation correctly.
                                    // There is a call to performAjaxValidation() commented in generated controller code.
                                    // See class documentation of CActiveForm for details on this.
                                    'enableAjaxValidation' => true,
                                    //'enableClientValidation'=>false,
                                    'clientOptions' => array(
                                        'validateOnSubmit' => true,
                                        'validateOnChange' => false,
                                        'validateOnType' => false,
                                        'afterValidate' => "js:function(form, data, hasError){
                                $.ajax({
                                    type: 'POST',
                                    url: form.attr( 'action' ),
                                    data: form.serialize(),
                                    success: function( response ) {
                                        if(response==1){
                                                $('#forgot-form').hide();
                                                form[0].reset();
                                                $.pnotify({
                                                title: 'Congratulations',
                                                 text: 'An email has been sent to you to change your password. It will expire in 2 hours.',
                                                 type: 'success',
                                                 history: false,
                                                 sticker: false
                                              });
                                        }else {
                                         $.pnotify({
                                                title: 'Error',
                                                 text: jQuery.parseJSON(response).Users_email,
                                                 type: 'error',
                                                 history: false,
                                                  sticker: false
                                              });
                                        }
                                    }
                                });
                                return false;
                              }
                            ",
                                    ),
                                ));
                                ?>
                                <p>Submit your email address and we’ll send you a link to reset your password.</p>
                                <?php echo $form->textField($subsModel, 'email', array('placeholder' => 'Input your email address')); ?>
                                <?php echo $form->error($subsModel, 'email', array('hideErrorMessage' => true)); ?>
                                <?php echo CHtml::submitButton('Submit', array('class' => "btn btn-signin btn-forgot")); ?>
                                <?php $this->endWidget(); ?>
                            </div>
                        </div>
                        <!--</div>-->
                    </div>
                </div>
            </div>
        </div>

        <script>
            function showForgotForm()
            {
                $('#signin_form').hide();
                $('#forgot-form').show();
                $('html').click(function(e) {
                    if ($('.forgot-form').has(e.target).length === 0) {
                        $('#forgot-form').hide();
                        $('#signin_form').removeAttr('style');
                    }
                });
            }
        </script>

        <div class="dimeter pull-left">&nbsp;&nbsp;OR&nbsp;&nbsp; </div>
        <div class="dropdown pull-left signup-tab">
            <a class="dropdown-toggle" data-toggle="dropdown" data-target="#" href="#">Register</a>
            <?php echo $this->renderPartial('//layouts/_signup') ?>
        </div>
    </div>
<?php else: ?>
    <ul class="my-account-ul pull-right">
        <li class="dropdown">
            <?php
            $config = array(
                "base_url" => "http://voithan.com/demo/shampoo/frontend/site/oauth",
                "providers" => array(
                    "Google" => array(
                        "enabled" => true,
                        "keys" => array("id" => "848332076147.apps.googleusercontent.com", "secret" => "CWmBOLwwdAOvDn3uiY_jgqG3")
                    ),
                    "Facebook" => array(
                        "enabled" => true,
                        "keys" => array("id" => "1419559608266256", "secret" => "a7d5ec135d7ba4743b692d306229b971")
                    ),
                    "Twitter" => array(
                        "enabled" => true,
                        "keys" => array("key" => "kogzQ097RksjrPkICyDA", "secret" => "rf2xffNJpKr3NkVFNUrZdQ1BoyjHgsWUoBmZZk9PfU")
                    ),
                ),
                // if you want to enable logging, set 'debug_mode' to true  then provide a writable file by the web server on "debug_file"
                "debug_mode" => false,
                "debug_file" => ""
            );
            ?>
            <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                <?php echo $this->user->name ?><b class="caret"></b></a>
            <ul class="dropdown-menu">
                <li><a href="<?php echo $this->createUrl('account/index') ?>"><i class="icon-user"></i> My Account</a></li>
    <!--                <li><a href="#"><i class="icon-check"></i> Your Requests</a></li>
                <li><a href="#"><i class="icon-envelope"></i> <span class="text"> Messages </span><span class="badge badge-info">4</span></a></li>-->
                <li class="divider"></li>
                <li><a href="<?php echo $this->createUrl('site/logout') ?>"><i class="icon-off"></i> Log Out</a></li>
            </ul>
        </li>
    </ul>
<?php endif; ?>
<script>
    function open_signup()
    {
        $('.signup-tab').addClass('open');
        $('.login-tab').removeClass('open');
        $('#forgot-form').removeAttr('style');
    }
//    function open_login()
//    {
//        $('.signup-tab').removeClass('open');
//        $('.login-tab').addClass('open');
//    }

    function login(reload)
    {
        $.post('<?php echo $this->createUrl('site/login'); ?>', {
            email: $("#UserLoginForm_email").val(),
            password: $("#UserLoginForm_password").val()
        }, function(data) {
            if (data.status == "ok") {
                setTimeout(function() {
                    if (reload == 1)
                        location.reload();
                    else
                        window.location = '<?php echo Yii::app()->homeUrl ?>';
                }, 1000);
            } else {
                $.each(data, function(key, val) {
                    $("#login-form #" + key + "_em_").text(val);
                    $("#login-form #" + key + "_em_").parent().addClass("error");
                    $("#login-form #" + key + "_em_").show();
                });
            }
        }, 'json');
    }

    function signup()
    {
        $.post('<?php echo $this->createUrl('site/signup'); ?>', {
            firstname: $("#UserSignUp_firstname").val(),
            lastname: $("#UserSignUp_lastname").val(),
            email: $("#UserSignUp_email").val(),
            mobile_phone: $("#UserSignUp_mobile_phone").val(),
            new_pass: $("#UserSignUp_new_pass").val(),
            repassword: $("#UserSignUp_repassword").val(),
            birthday: $("#UserSignUp_birthday").val(),
            subscribe: $("#UserSignUp_subscribe").is(':checked') ? 1 : 0,
        }, function(data) {
            $("#signup-form .input-medium").removeClass('error-field').removeAttr('title');
            if (data.status == "ok") {
                $('#register_form').removeAttr('style');
                $('.signup-tab').removeClass('open');
                $('#signup-form')[0].reset();
                $.pnotify({
                    title: '<?php echo Yii::t('frontend','Congratulations')?>',
                    text: '<?php echo Yii::t('frontend','Signup successfully, A welcome email has been sent to you. Thank you!')?>',
                    type: 'success',
                    history: false,
                    sticker: false
                });
            } else {
                $.pnotify({
                    title: '<?php echo Yii::t('frontend','Error')?>',
                    text: '<?php echo Yii::t('frontend','Please fill correct information in red field and try again!')?>',
                    type: 'error',
                    history: false,
                    sticker: false
                });
                $.each(data, function(key, val) {
                    $("#signup-form #" + key).addClass('error-field').attr('data-original-title', val);
                    <?php cs()->registerScriptFile(themeUrl() . '/assets/js/jquery.pnotify.min.js'); ?>
                    $('.input-medium').tooltip();
                });
            }
        }, 'json');
    }
</script>
